﻿using System.Windows.Forms;
using VNPT.Utility.Profile;
using System.IO;
using System.Linq;
using System;
using DevExpress.XtraWizard;
using VNPT.Utility.Oracle;
using System.Collections.Generic;
using VNPT.Data;
using VNPT;

namespace VNPT.DoiSoat
{
    public partial class fMain : DevExpress.XtraEditors.XtraForm
    {
        public fMain()
        {
            InitializeComponent();
        }

        Xml xml = new Xml();
        private void LoadSetting()
        {
            btnThuMucBMT.EditValue = DoiSoatSetting.Folder_BMT;
            btnThuMucTanLoi.EditValue = DoiSoatSetting.Folder_TANLOI;
            btnLuuTru.EditValue = DoiSoatSetting.Folder_LUU;

            comboboxNam.EditValue = DoiSoatSetting.NAM;
            comboboxThang.EditValue = DoiSoatSetting.THANG.ToString("D2");
           

            if (DataSetting.IS_SAVE)
            {
                chkSave.CheckState = CheckState.Checked;
                txtPass.EditValue = DataSetting.PWD;
            }
            else
            {
                chkSave.CheckState = CheckState.Unchecked;
            }
            txtPass.EditValue = DataSetting.PWD;
            txtUser.EditValue = DataSetting.UID;
            cmbTNS_Name.EditValue = DataSetting.TNS_NAME;

            LoadTSName();
            if (db==null)
            {
                this.pWelcome.AllowNext = false;
            }
        }
        private void LoadTSName()
        {
            TNSNamesReader tsn = new TNSNamesReader();
            List<string> li = new List<string>();

            cmbTNS_Name.Properties.Items.Clear();
            foreach (var item in tsn.GetOracleHomes())
            {
                li.AddRange(tsn.LoadTNSNames(item));
            }
            cmbTNS_Name.Properties.Items.AddRange(li);
        }
        private void SaveSetting()
        {
            if (!Directory.Exists(btnThuMucBMT.EditValue.ToString()))
            {
                DevExpress.XtraEditors.XtraMessageBox.Show("Thư mục lưu cước host BMT không tồn tại!", "Thông báo!", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            if (!Directory.Exists(btnThuMucTanLoi.EditValue.ToString()))
            {
                DevExpress.XtraEditors.XtraMessageBox.Show("Thư mục lưu cước host Tân lợi không tồn tại!", "Thông báo!", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            if (!Directory.Exists(btnLuuTru.EditValue.ToString()))
            {
                DevExpress.XtraEditors.XtraMessageBox.Show("Thư mục lưu kết quả không tồn tại!", "Thông báo!", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            DoiSoatSetting.Folder_BMT = btnThuMucBMT.EditValue.ToString();
            DoiSoatSetting.Folder_TANLOI = btnThuMucTanLoi.EditValue.ToString();
            DoiSoatSetting.Folder_LUU = btnLuuTru.EditValue.ToString();

            DoiSoatSetting.NAM = int.Parse("0" + comboboxNam.EditValue.ToString());
            DoiSoatSetting.THANG = int.Parse("0" + comboboxThang.EditValue.ToString());
            SaveData();
        }
        void SaveData()
        {
            if (chkSave.CheckState == CheckState.Checked)
            {
                DataSetting.IS_SAVE = true;
                DataSetting.PWD = txtPass.EditValue.ToString();
            }
            else
            {
                DataSetting.IS_SAVE = false;
                DataSetting.PWD = "";
            }

            DataSetting.UID = txtUser.EditValue.ToString();
            DataSetting.TNS_NAME = cmbTNS_Name.EditValue.ToString();
        }
        string strCnn = "Data Source={0};User Id={1};Password={2};";
        private bool CheckConnect()
        {
            var cnn = string.Format(strCnn, cmbTNS_Name.EditValue.ToString(), txtUser.EditValue.ToString(), txtPass.EditValue);
            DbNetData Db = new DbNetData(cnn, DataProvider.OracleClient);
            try
            {
                using (new DevExpress.Utils.WaitDialogForm("Đang kết nối, vui lòng chờ...","Vui lòng đợi trong giây lát!"))
                {
                    
                    Db.Open();
                    db = Db;
                    db.CloseConnectionOnError = false;
                    db.ShowConnectionStringOnError = true;
                }
                
                return true;
            }
            catch (Exception ex)
            {
                DevExpress.XtraEditors.XtraMessageBox.Show("Không thể kết nối!\n" + ex.Message, "Thông báo!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                db = null;
                return false;
            }
        }
        private void wzControl_SelectedPageChanged(object sender, DevExpress.XtraWizard.WizardPageChangedEventArgs e)
        {
            if (e.Page.Controls.Count>=1)
            {
                var t = e.Page.Controls[0] as Core.uBase;
                if (t != null)
                {
                    t.DbServer = db;
                    t.LoadSetting();
                    
                }
            }
            if (e.Page.Name == "pWelcome")
            {
                LoadSetting();
            }
        }

        private void wzControl_NextClick(object sender, WizardCommandButtonClickEventArgs e)
        {
            if (e.Page.Controls.Count >= 1)
            {
                var t = e.Page.Controls[0] as Core.uBase;
                if (t != null)
                {
                    t.SaveSetting();
                }
            }

            if (e.Page.Name == "pWelcome")
            {
                SaveSetting();
            }
        }

        private void hpLink1_OpenLink(object sender, DevExpress.XtraEditors.Controls.OpenLinkEventArgs e)
        {
            if (db==null)
            {
                DevExpress.XtraEditors.XtraMessageBox.Show("Vui lòng kết nối server trước khi thực hiện các thao tác!", "Thông báo!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            var t= (sender as DevExpress.XtraEditors.HyperLinkEdit);
            VNPT.Core.uBase u= new Core.uBase();
            u.DbServer = db;
            switch (t.Name)
            {
                case "hpLink1":
                    u = new uDauCuoi();
                    break;
                case "hpLink2":
                    u = new uNoiFile();
                    break;
                case "hpLink3":
                    u = new fDbf2Oracle();
                    break;
                case "hpLink4":
                    u = new uDongBoMaMang();
                    break;
                case "hpLink5":
                    u = new uCapNhatDVBB_MaMang();
                    break;
                case "hpLink6":
                    u = new uLocRoute();
                    break;
                case "hpLink7":
                    u = new uLayGTGT();
                    break;
                case "hpLink8":
                    u = new uFTPExplorer();
                    break;
                case "hpLink9":
                   u = new uNoiFileGphone();
                    break;
                case "hpLink10":
                    u = new u178Viettel();
                    break;
                case "hpLink11":
                    new fDoiSoat(db).ShowDialog();
                    u = null;
                    break;

                case "hpLink12":
                    new fBaoCao(db).ShowDialog();
                    u = null;
                    break;
                default:
                    break;
            }
            if (u!=null)
            {
                u.DbServer = db;
                new fSelectPage(u,t.Text).ShowDialog();
            }
            
        }
        DbNetData db;
        private void btnConnect_Click(object sender, EventArgs e)
        {
            SaveData();
            SaveSetting();

            if (this.btnConnect.Text=="Connect")
            {
                if (CheckConnect())
                {
                    //DevExpress.XtraEditors.XtraMessageBox.Show("Đã kết nối thành công!");
                    this.btnConnect.Text = "Disconnect";
                    
                    txtUser.Enabled = false;
                    txtPass.Enabled = false;
                    cmbTNS_Name.Enabled = false;

                    btnLuuTru.Enabled = false;
                    btnThuMucBMT.Enabled = false;
                    btnThuMucTanLoi.Enabled = false;

                    comboboxNam.Enabled = false;
                    comboboxThang.Enabled = false;

                    this.pWelcome.AllowNext = true;
                    Data.DataConnect.DbServer = db;
                }
            }
            else
            {
                db = null;
                this.btnConnect.Text = "Connect";
                txtUser.Enabled = !false;
                txtPass.Enabled = !false;
                cmbTNS_Name.Enabled = !false;
                this.pWelcome.AllowNext = !true;


                btnLuuTru.Enabled = !false;
                btnThuMucBMT.Enabled = !false;
                btnThuMucTanLoi.Enabled = !false;

                comboboxNam.Enabled = !false;
                comboboxThang.Enabled = !false;
            }
            
        }
    }
}

